<?php 
$categories = $this->categories;
$article = $this->article;

//Đường link đến cần theo cú pháp article/edit?id=<ID>
//Nếu người dùng không nhập thì article sẽ null
//Đưa ra thông báo :
if($article == null)
{
	echo 'Need an ID param in link. <br>
	Struct : <b>article/edit?id=&lt;ID&gt;</b> <br>
	&lt;ID&gt; : is ID of article you want to edit.';
	return;
}
else if($article['ID']==''){
	echo 'Invalid ID.';
	return;
}

//Add category to combobox, start with level = 1 and parentID = 0
function AddCategory($cats, $level, $parentID, $categoryIDofArticle)
{
	foreach($cats as $cat)
	{
		if($cat['parentid']==$parentID && $cat['level']==$level)
		{
			//Chọn thẻ option nào có thuộc tính selected="true"
			if($cat['id']==$categoryIDofArticle){
				$name = "<option selected>";
			}
			else{
				$name = "<option >";
			}
			
			//Thụt vào tùy theo cấp độ
			for($i = 0; $i<$level;$i++)
			{
				$name.="&nbsp&nbsp&nbsp";
			}
			$name.="|_&nbsp";
			$name.=$cat['name'];
			$name.="</option>";
			
			echo $name;
			AddCategory($cats, $level+1, $cat['id'], $categoryIDofArticle);
		}
	}
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit article</title>

<script src="<?php echo $this->baseUrl();?>/jquery/jqueryui/jquery-1.6.4.min.js"></script>
<script src="<?php echo $this->baseUrl();?>/jquery/jqueryui/jquery-ui-1.8.16.custom.min.js"></script>
<script src="<?php echo $this->baseUrl();?>/jquery/jqueryui/jquery-ui-timepicker-addon.js"></script>
<!--  <script src="<?php echo $this->baseUrl();?>/jquery/ui/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="jqueryui/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="jqueryui/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="jqueryui/jquery-ui-timepicker-addon.js"></script>  -->
<link rel="stylesheet" href="<?php echo $this->baseUrl();?>/jquery/jqueryui/jquery-ui-1.8.16.custom.css"/>


<script type="text/javascript">

function OnSaveClick(form)
{
    getContentToHiddenInput();
	form.action='edit-done';
    form.submit();
    return true;
}
function OnChooseClicked()
{
	window.open("../user/choose", 'mypopup' , 'width=400, height=300, toolbar=no, scrollbars=no, resizable=no, status=no, toolbar=no, menubar=no, location=no');
}

</script>

<script>
$(function()
{
	$("#tCreatedDate").datetimepicker({timeFormat: 'hh:mm:ss', dateFormat: 'yy-mm-dd'});
});

$(function()
{
	$("#tStartPublishing").datetimepicker({timeFormat: 'hh:mm:ss', dateFormat: 'yy-mm-dd'});
});

$(function()
{
	$("#tFinishPublishing").datetimepicker({timeFormat: 'hh:mm:ss', dateFormat: 'yy-mm-dd'});
});

</script>

</head>



<body>
<form name="myform" method="post">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="50%"><strong>Article manager : Edit article</strong></td>
    <td width="50%" align="right"><input type="submit" name="bSave" id="bSave" value="Save" onclick="OnSaveClick(this.form)"/>
      <input type="submit" name="bCancel" id="bCancel" value="Cancel" onclick="OnCancelClick()"/></td>
  </tr>
  <tr>
    <td rowspan="3"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Title*</td>
        <td><label for="tTitle"></label>
          <input type="text" name="tTitle" id="tTitle" value="<?php echo $article['Title'];?>" /></td>
      </tr>
      <tr>
        <td>Alias</td>
        <td><label for="tAlias"></label>
          <input type="text" name="tAlias" id="tAlias" value="<?php echo $article['Alias'];?>"/></td>
      </tr>
      <tr>
        <td>Category*</td>
        <td><label for="sCategory"></label>
          <select name="sCategory" id="sCategory" value="<?php echo $article['CategoryID'];?>">
          	<?php AddCategory($categories, 1, 0, $article['CategoryID']);?>
          </select></td>
      </tr>
      <tr>
        <td>State          </td>
        <td><label for="sState"></label>
          <select name="sState" id="sState">
            <option <?php if($article['State']==0) echo" selected=\"true\""?>>Published</option>
            <option <?php if($article['State']==1) echo" selected=\"true\""?>>Unpublished</option>
            <option <?php if($article['State']==2) echo" selected=\"true\""?>>Archived</option>
            <option <?php if($article['State']==3) echo" selected=\"true\""?>>Trashed</option>
          </select></td>
      </tr>
      <tr>
        <td>Access</td>
        <td><label for="sAccess"></label>
          <select name="sAccess" id="sAccess">
            <option <?php if($article['Access']==0) echo" selected=\"true\""?>>Public</option>
            <option <?php if($article['Access']==1) echo" selected=\"true\""?>>Registered</option>
            <option <?php if($article['Access']==2) echo" selected=\"true\""?>>Special</option>
          </select></td>
      </tr>
      <tr>
        <td>ID</td>
        <td><?php echo $article['ID'];?></td>
      </tr>
      <tr>
        <td colspan="2">
			<p>Article :</p>
          	<?php include_once('RichTextEditor/editor.phtml'); ?></td>
        </tr>
      <tr>
        <td colspan="2">&nbsp;</td>
        </tr>
    </table></td>
    <td>Publishing Option
<table width="200" border="0">
  <tr>
        <td>Created by</td>
        <td><label for="tCreatedBy"></label>
          <input type="text" name="tCreatedBy" id="tCreatedBy" value="<?php echo $article['CreatedByName'];?>"/></td>
      </tr>
      <tr>
        <td colspan="2"><input type="button" name="bSelectUser" id="bSelectUser" value="Select user" onclick="OnChooseClicked()"/></td>
        </tr>
      <tr>
        <td>Created date</td>
        <td><label for="tCreatedDate"></label>
          <input type="text" name="tCreatedDate" id="tCreatedDate" value="<?php echo $article['CreatedTime'];?>"/></td>
      </tr>
      <tr>
        <td>Start publishing</td>
        <td><label for="tStartPublishing"></label>
          <input type="text" name="tStartPublishing" id="tStartPublishing" value="<?php echo $article['PublicUp'];?>"/></td>
      </tr>
      <tr>
        <td>Finish publishing</td>
        <td><label for="tFinishPublishing"></label>
          <input type="text" name="tFinishPublishing" id="tFinishPublishing" value="<?php echo $article['PublicDown'];?>"/></td>
      </tr>
  </table></td>
  </tr>
  <tr>
    <td height="150"><p>Metadata Option</p>
      <p>Metadata description:
        <label for="tMetaDes"></label>
        <textarea name="tMetaDes" id="tMetaDes" cols="45" rows="5"></textarea>
      </p>
      <p>Metadata keyword :
        <label for="tMetaKeyword"></label>
        <textarea name="tMetaKeyword" id="tMetaKeyword" cols="45" rows="5"></textarea>
      </p></td>
  </tr>
</table>
<!-- Đưa ID sang trang edit-done  -->
<input type="hidden" id="id" name="id" value="<?php echo $article['ID'];?>"/>
</form>
</body>
</html>

<script>
	$("#tMetaDes").attr("value","<?php echo $article['MetaDes'];?>");
	$("#tMetaKeyword").attr("value","<?php echo $article['MetaKeyword'];?>");
	setContentForRichTextbox('<?php echo $article['IntroContent'].'<hr>'.$article['Content'];?>');
</script>